import 'package:flutter/material.dart';
import 'package:flutter_swiper_null_safety_flutter3/flutter_swiper_null_safety_flutter3.dart';
import '../resources.dart';

typedef BannerCallback = void Function();

class BannerPage<T> extends StatefulWidget {
  final List<T> banners;
  final double bannerHeight;
  final IndexedWidgetBuilder itemBuilder;
  final Widget? emptyItem;
  final bool light;

  const BannerPage(
      {super.key,
      this.bannerHeight = 152,
      this.light = true,
      required this.banners,
      required this.itemBuilder,
      this.emptyItem});

  @override
  State<StatefulWidget> createState() => _BannerState();
}

class _BannerState extends State<BannerPage> {
  bool isResume = false;
  int current = 0;

  @override
  void didChangeDependencies() {
    super.didChangeDependencies();
    setState(() {
      isResume = !isResume;
    });
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: widget.bannerHeight.ss,
      child: Swiper(
        // 横向
        scrollDirection: Axis.horizontal,
        // 布局构建
        itemBuilder: widget.banners.isNotEmpty
            ? widget.itemBuilder
            : (context, index) {
                return widget.emptyItem ?? const SizedBox();
              },
        //条目个数
        itemCount: widget.banners.isNotEmpty ? widget.banners.length : 1,
        // 自动翻页
        autoplay: widget.banners.length > 1 && isResume,
        // 分页指示
        pagination: MyPoint(),
        //点击事件
        onTap: (index) {
          debugPrint(" 点击 $index");
        },
        // 相邻子条目视窗比例
        viewportFraction: 1,
        // 布局方式
        //layout: SwiperLayout.STACK,
        // 用户进行操作时停止自动翻页
        autoplayDisableOnInteraction: true,
        // 无线轮播
        loop: widget.banners.isNotEmpty && isResume,
        //当前条目的缩放比例
        scale: 1,
        index: current,
        onIndexChanged: (int index) {
          current = index;
        },
      ),
    );
  }
}

class MyPoint extends SwiperPlugin {
  static const SwiperPlugin dots = DotSwiperPaginationBuilder();

  static const SwiperPlugin fraction = FractionPaginationBuilder();

  static const SwiperPlugin rect = RectSwiperPaginationBuilder();

  final bool light;

  MyPoint({this.light = true});

  @override
  Widget build(BuildContext context, SwiperPluginConfig config) {
    Widget child = Container(
        //点之间的间距
        margin: const EdgeInsets.fromLTRB(0, 0, 0, 0),
        color: Colors.transparent,
        child: BannerSwiperPaginationBuilder(
                // 点之间的间隔
                space: 5,
                // 没选中时的大小
                size: const Size(24, 5),
                // 选中时的大小
                activeSize: const Size(24, 5),
                // 没选中时的颜色
                color:
                    light ? ColorUtils.color_CCCCCC : ColorUtils.color_FFFFFF,
                //选中时的颜色
                activeColor:
                    light ? ColorUtils.app_color : ColorUtils.color_D4AC5B)
            .build(context, config));
    if (!config.outer) {
      child = Align(
        alignment: Alignment.bottomCenter,
        child: child,
      );
    }
    return child;
  }
}

class BannerSwiperPaginationBuilder extends SwiperPlugin {
  ///color when current index,if set null , will be Theme.of(context).primaryColor
  final Color? activeColor;

  ///,if set null , will be Theme.of(context).scaffoldBackgroundColor
  final Color? color;

  ///Size of the rect when activate
  final Size activeSize;

  ///Size of the rect
  final Size size;

  /// Space between rects
  final double space;

  final double radius;

  final Key? key;

  const BannerSwiperPaginationBuilder(
      {this.activeColor,
      this.color,
      this.key,
      this.size = const Size(10.0, 2.0),
      this.activeSize = const Size(10.0, 2.0),
      this.space = 3.0,
      this.radius = 3.0});

  @override
  Widget build(BuildContext context, SwiperPluginConfig config) {
    ThemeData themeData = Theme.of(context);
    Color activeColor = this.activeColor ?? themeData.primaryColor;
    Color color = this.color ?? themeData.scaffoldBackgroundColor;

    List<Widget> list = [];

    if (config.itemCount > 20) {
      print(
          "The itemCount is too big, we suggest use FractionPaginationBuilder instead of DotSwiperPaginationBuilder in this sitituation");
    }

    int itemCount = config.itemCount;
    int activeIndex = config.activeIndex;

    for (int i = 0; i < itemCount; ++i) {
      bool active = i == activeIndex;
      Size size = active ? activeSize : this.size;
      list.add(Container(
        width: size.width,
        height: size.height,
        decoration: BoxDecoration(
            color: active ? activeColor : color,
            borderRadius: BorderRadius.circular(radius)),
        key: Key("pagination_$i"),
        margin: EdgeInsets.all(space),
      ));
    }

    if (config.scrollDirection == Axis.vertical) {
      return Column(
        key: key,
        mainAxisSize: MainAxisSize.min,
        children: list,
      );
    } else {
      return Row(
        key: key,
        mainAxisSize: MainAxisSize.min,
        children: list,
      );
    }
  }
}
